<template>
  <el-dialog
    :modelValue="visible"
    @update:modelValue="$emit('update:visible', $event)"
    title="选择客户"
    width="70%"
    :close-on-click-modal="false"
    @close="handleClose">
    <div class="customer-select">
      <!-- 搜索区域 -->
      <div class="search-area">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="客户编码">
            <el-input v-model="searchForm.custCode" placeholder="请输入客户编码" clearable />
          </el-form-item>
          <el-form-item label="客户名称">
            <el-input v-model="searchForm.custName" placeholder="请输入客户名称" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格区域 -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading"
        height="400px"
        @row-click="handleRowClick"
        :header-cell-style="{ background: '#f5f7fa' }">
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="custCode" label="客户编码" min-width="120" show-overflow-tooltip />
        <el-table-column prop="custName" label="客户名称" min-width="180" show-overflow-tooltip />
        <el-table-column prop="contactPerson" label="联系人" min-width="120" show-overflow-tooltip />
        <el-table-column prop="contactPhone" label="联系电话" min-width="120" show-overflow-tooltip />
        <el-table-column prop="address" label="地址" min-width="200" show-overflow-tooltip />
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import http from '@/api/http'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:visible', 'select'])

// 搜索表单
const searchForm = reactive({
  custCode: '',
  custName: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取客户列表
const fetchCustomers = async () => {
  loading.value = true
  try {
    const params = {
      PageIndex: currentPage.value,
      PageSize: pageSize.value
    }

    if (searchForm.custCode) {
      params.CustCode = searchForm.custCode.trim()
    }
    if (searchForm.custName) {
      params.CustName = searchForm.custName.trim()
    }

    const response = await http.get('/api/Customer/GetCustomerList', params)
    
    if (response?.data?.items && Array.isArray(response.data.items)) {
      tableData.value = response.data.items
      total.value = response.data.total || 0
    } else {
      tableData.value = []
      total.value = 0
    }
  } catch (error) {
    console.error('获取客户列表失败:', error)
    ElMessage.error('获取客户列表失败')
    tableData.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchCustomers()
}

// 重置
const handleReset = () => {
  searchForm.custCode = ''
  searchForm.custName = ''
  currentPage.value = 1
  fetchCustomers()
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  fetchCustomers()
}

// 页码改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchCustomers()
}

// 行点击
const handleRowClick = (row) => {
  emit('select', row)
  emit('update:visible', false)
}

// 关闭对话框
const handleClose = () => {
  emit('update:visible', false)
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCustomers()
})
</script>

<style scoped>
.customer-select {
  padding: 10px;
}

.search-area {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 